/*
 * Copyright 2010 Biz-e (info@biz-e.nl)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.biz_e.gwt.baselib.ui.client.widgets;

import java.util.ArrayList;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;

/**
 * Simple clickable image that switches images depending on selected state
 * 
 * @author Joost A. Bloemsma
 * 
 */
public class ImageToggleButton extends Composite implements ClickHandler {
    private boolean selected = false;
    private boolean deSelectable = true;
    private ArrayList<ImageToggleButtonListener> listeners = new ArrayList<ImageToggleButtonListener>();
    private String unSelectedUrl;
    private String selectedUrl;
    private ImageResource selectedResource;
    private ImageResource unSelectedResource;
    private boolean useUrl = true;
    private Image image;

    /**
     * Constructs a new ImageToggleButton instance
     * 
     * @param selectedUrl
     * @param unSelectedUrl
     * @param listener
     */
    public ImageToggleButton(String selectedUrl, String unSelectedUrl,
            ImageToggleButtonListener listener) {
        this(unSelectedUrl, selectedUrl);
        this.addListener(listener);
    }

    /**
     * Constructs a new ImageToggleButton instance
     * 
     * @param selectedUrl
     * @param unSelectedUrl
     */
    public ImageToggleButton(String selectedUrl, String unSelectedUrl) {
        this.setUnSelectedUrl(unSelectedUrl);
        this.setSelectedUrl(selectedUrl);
        this.init();
    }

    /**
     * Constructs a new ImageToggleButton instance
     * 
     * @param selectedResource
     * @param unSelectedResource
     * @param listener
     */
    public ImageToggleButton(ImageResource selectedResource,
            ImageResource unSelectedResource, ImageToggleButtonListener listener) {
        this(selectedResource, unSelectedResource);
        this.addListener(listener);
    }

    /**
     * Constructs a new ImageToggleButton instance
     * 
     * @param selectedResource
     * @param unSelectedResource
     */
    public ImageToggleButton(ImageResource selectedResource,
            ImageResource unSelectedResource) {
        this.setSelectedResource(selectedResource);
        this.setUnSelectedResource(unSelectedResource);
        this.init();
    }

    /**
     * @param listener
     */
    public void addListener(ImageToggleButtonListener listener) {
        if ((listener != null) & (!this.listeners.contains(listener))) {
            this.listeners.add(listener);
        }
    }

    /**
     * @param listener
     */
    public void removeListener(ImageToggleButtonListener listener) {
        if ((listener != null) & (this.listeners.contains(listener))) {
            this.listeners.remove(listener);
        }
    }

    /**
     * @return the selected
     */
    public boolean isSelected() {
        return selected;
    }

    /**
     * @param selected
     *            the selected to set
     */
    public void setSelected(boolean selected) {
        this.selected = selected;
        if (selected) {
            if (useUrl) {
                image.setUrl(selectedUrl);
            } else {
                image.setResource(selectedResource);
            }
        } else {
            if (useUrl) {
                image.setUrl(unSelectedUrl);
            } else {
                image.setResource(unSelectedResource);
            }
        }
    }

    /**
     * @return the deSelectable
     */
    public boolean isDeSelectable() {
        return deSelectable;
    }

    /**
     * @param deSelectable
     *            the deSelectable to set
     */
    public void setDeSelectable(boolean deSelectable) {
        this.deSelectable = deSelectable;
    }

    /**
     * @return the unSelectedUrl
     */
    public String getUnSelectedUrl() {
        return unSelectedUrl;
    }

    /**
     * @param unSelectedUrl
     *            the unSelectedUrl to set
     */
    public void setUnSelectedUrl(String unSelectedUrl) {
        this.unSelectedUrl = unSelectedUrl;
    }

    /**
     * @return the selectedUrl
     */
    public String getSelectedUrl() {
        return selectedUrl;
    }

    /**
     * @param selectedUrl
     *            the selectedUrl to set
     */
    public void setSelectedUrl(String selectedUrl) {
        this.selectedUrl = selectedUrl;
    }

    /**
     * @return the selectedResource
     */
    public ImageResource getSelectedResource() {
        return selectedResource;
    }

    /**
     * @param selectedResource
     *            the selectedResource to set
     */
    public void setSelectedResource(ImageResource selectedResource) {
        this.selectedResource = selectedResource;
    }

    /**
     * @return the unSelectedResource
     */
    public ImageResource getUnSelectedResource() {
        return unSelectedResource;
    }

    /**
     * @param unSelectedResource
     *            the unSelectedResource to set
     */
    public void setUnSelectedResource(ImageResource unSelectedResource) {
        this.unSelectedResource = unSelectedResource;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void onClick(ClickEvent event) {
        if (deSelectable || !selected) {
            this.setSelected(!selected);
        }
        for (ImageToggleButtonListener listener : listeners) {
            if (listener != null)
                listener.onClick(this);
        }
    }

    private void init() {
        image = new Image();
        this.initWidget(image);
        this.setSelected(false);
    }

    /**
     * TODO: Write type comment
     * 
     * @author Joost A. Bloemsma
     */
    public static interface ImageToggleButtonListener {
        /**
         * Is fired when the button is clicked
         * 
         * @param source
         *            The clicked button
         */
        public void onClick(ImageToggleButton source);
    }

}
